package com.bms.comm.cas;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.jasig.cas.client.validation.Assertion;
import org.springframework.security.cas.userdetails.AbstractCasAssertionUserDetailsService;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.UserDetails;

import com.bms.comm.MyUserDetails;

public class MyAuthenticationUserDetailsService extends AbstractCasAssertionUserDetailsService {
	
	private static final String NON_EXISTENT_PASSWORD_VALUE = "NO_PASSWORD";
	
	private String[] attributes; 
	
	@Override
	protected UserDetails loadUserDetails(Assertion assertion) {
		// TODO Auto-generated method stub
		List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
		com.bms.bean.User muser = new com.bms.bean.User();
		
		Object[] userFileds = new Object[this.attributes.length-1];
		int index = 0;
		for (String attribute : this.attributes) {
			Object value = assertion.getPrincipal().getAttributes().get(attribute);
			
			if (value == null) {
				continue;
			}
			
			if(index == 0){
				grantedAuthorities.add(new GrantedAuthorityImpl((String)value));
			}else{
				
				userFileds[index-1] = value;
			}
			
			index += 1;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		try{
			if(userFileds.length>0){
				muser.setUserId(Long.valueOf((String)userFileds[0]));            
				muser.setNickName((String)userFileds[1]);           
				muser.setPassWord((String)userFileds[2]);           
				muser.setUserEmail((String)(userFileds[3]==null?"":userFileds[3]));          
				muser.setMobileNo((String)(userFileds[4]==null?"":userFileds[4]));           
				muser.setBirthDay(sdf.parse((String)userFileds[5]));           
				muser.setUserSex(Integer.valueOf((String)(userFileds[6]==null?0:userFileds[6])));            
				muser.setRegistrationDate(sdf.parse((String)userFileds[7]));   
				muser.setUserStatus(Integer.valueOf((String)(userFileds[8]==null?0:userFileds[8])));         
				muser.setRoleId(Long.valueOf((String)userFileds[9]));             
				muser.setDepartId(Long.valueOf((String)(userFileds[10]==null?0:userFileds[10])));           
				muser.setOrgId(Long.valueOf((String)(userFileds[11]==null?0:userFileds[11])));              
				muser.setGroupId(Long.valueOf((String)(userFileds[12]==null?0:userFileds[12])));            
				muser.setCreateDate(sdf.parse((String)userFileds[13]));         
				muser.setUserPic300((String)(userFileds[14]==null?"":userFileds[14]));         
				muser.setModifyDate(sdf.parse((String)userFileds[15]));         
				muser.setUserPic30((String)(userFileds[16]==null?"":userFileds[16]));    
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		MyUserDetails userDetails = new MyUserDetails(muser.getNickName(),NON_EXISTENT_PASSWORD_VALUE,grantedAuthorities,muser);;  
//		User user = new User(assertion.getPrincipal().getName(), NON_EXISTENT_PASSWORD_VALUE,
//				true, true, true, true, grantedAuthorities);
		return userDetails; 
	}

	public String[] getAttributes() {
		return attributes;
	}

	public void setAttributes(String[] attributes) {
		this.attributes = attributes;
	}

	
}
